﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.UI;

/// <summary>
/// Summary description for CNavigator
/// </summary>
public class CNavigator
{
	public CNavigator()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static string CreateNavigator(int page, int count, int totalcount,Page Page,CMyLink lnkNav)
    {
        int pcount = totalcount / count;
        int reminder = totalcount % count;
        if (reminder > 0)
        {
            pcount++;
        }
        if (pcount == 0)
        {
            pcount++;
        }
        StringBuilder sb = new StringBuilder();
        sb.Append("<dt class=\"current\">" + page.ToString() + "</dt>");
        int ltmp = 0;
        int rtmp = 0;
        for (int i = 1; i < 4; i++)
        {
            if (page - i >= 1)
            {
                sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - i).ToString()) + "\">" + (page - i).ToString() + "</a></dt>");
                ltmp++;
            }
            if (page + i <= pcount)
            {
                sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + i).ToString()) + "\">" + (page + i).ToString() + "</a></dt>");
                rtmp++;
            }
        }
        if (ltmp + rtmp < 6)
        {
            int left = ltmp + 1;
            int right = rtmp + 1;
            if (page - 3 > 1)
            {
                for (int i = left; i <= 6 - rtmp; i++)
                {
                    if (page - i >= 1)
                    {
                        sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - i).ToString()) + "\">" + (page - i).ToString() + "</a></dt>");
                        ltmp++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else if (page + 3 < pcount)
            {
                for (int i = right; i <= 6 - ltmp; i++)
                {
                    if (page + i <= pcount)
                    {
                        sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + i).ToString()) + "\">" + (page + i).ToString() + "</a></dt>");
                        rtmp++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        if (page - ltmp > 1)
        {
            sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - ltmp - 1).ToString()) + "\">...</a></dt>");
        }
        //首页、前一页
        if (page > 1)
        {
            sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page - 1).ToString()) + "\">|&lt;</a></dt>");
            sb.Insert(0, "<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, "1") + "\">|&lt;&lt;</a></dt>");
        }
        else
        {
            sb.Insert(0, "<dt>|&lt;</dt>");
            sb.Insert(0, "<dt>|&lt;&lt;</dt>");
        }

        if (page + rtmp < pcount)
        {
            sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + rtmp + 1).ToString()) + "\">...</a></dt>");
        }
        //末页、下一页
        if (page < pcount)
        {
            sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, (page + 1).ToString()) + "\">&gt;|</a></dt>");
            sb.Append("<dt><a href=\"" + Page.ClientScript.GetPostBackClientHyperlink(lnkNav, pcount.ToString()) + "\">&gt;&gt;|</a></dt>");
        }
        else
        {
            sb.Append("<dt>&gt;|</dt>");
            sb.Append("<dt>&gt;&gt;|</dt>");
        }
        sb.Insert(0, "<dl id=\"nav\">");
        sb.Append("</dl>");
        return sb.ToString();
    }
}